package self.rss.proto.util;

import java.util.LinkedList;
import self.rss.proto.R;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Quiz extends Activity {

	
	
	private DatabaseManager QuizHelper;
	private SQLiteDatabase mDatenbank;
	private TextView tvFrage;
	private String Frage;
	private String Antwort1;
	private String Antwort2;
	private String Antwort3;
	private String Antwort4;
	private String AntwortMaster;
	private int id;
	private LinkedList<Integer> ids;
	private int next;


	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.quiz);
		QuizHelper = new DatabaseManager(this);
		mDatenbank = QuizHelper.getReadableDatabase();
		ids=new LinkedList<Integer>();
		
		countRows();
		if(ids.size()<=0){
		fillDatabase();
		}
		ids.clear();
		countRows();
		next=1;
		Toast.makeText(this, "Anzahl :"+ids.size(), 30).show();
		ladeDaten();
		ActivityRegistry.register(this);

	}
	
	
	public void textViewMaker() {


		tvFrage = (TextView) findViewById(R.id.tx_frage);
		Typeface font = Typeface.createFromAsset(getAssets(), "League_Gothic.otf");
		tvFrage.setTypeface(font);
		tvFrage.setText(Frage);
		tvFrage.setText(Frage);
		tvFrage.setSingleLine(false);
		View antwort1 = (View) findViewById(R.id.sf_antwort1);
		((TextView) antwort1).setText(Antwort1);
		View antwort2 = (View) findViewById(R.id.sf_antwort2);
		((TextView) antwort2).setText(Antwort2);
		View antwort3 = (View) findViewById(R.id.sf_antwort3);
		((TextView) antwort3).setText(Antwort3);
		View antwort4 = (View) findViewById(R.id.sf_antwort4);
		((TextView) antwort4).setText(Antwort4);

	}
	private void ladeDaten() {

		Cursor myCursor = mDatenbank.query("quiz", // Tabellenname
				new String[] { // anzuzeigende Spalten
				"_id", "frage", "antwort1, antwort2, antwort3, antwort4, antwortmaster" }, "_id="+next, // WHERE (z.B. "_id = ?")
				null, // WHERE-Argumente (f�r "?")
				null, // GROUP BY
				null, // HAVING
				null // ORDER BY
				);

		 startManagingCursor(myCursor);
		 myCursor.moveToFirst();
		
		 
	if (myCursor!=null){
		if(myCursor.isFirst()){
			id = myCursor.getInt(0);
			Frage = myCursor.getString(1);
			Antwort1 = myCursor.getString(2);
			Antwort2 = myCursor.getString(3);
			Antwort3 = myCursor.getString(4);
			Antwort4 = myCursor.getString(5);
			AntwortMaster = myCursor.getString(6);
			textViewMaker();
		}
	}

	}

    private void countRows() {

		Cursor myCursor = mDatenbank.query("quiz", // Tabellenname
				new String[] { // anzuzeigende Spalten
				"_id" }, null, // WHERE (z.B. "_id = ?")
				null, // WHERE-Argumente (f�r "?")
				null, // GROUP BY
				null, // HAVING
				null // ORDER BY
				);
		myCursor.moveToFirst();

		while (myCursor.isAfterLast() == false) {

			id = myCursor.getInt(0);
			ids.add(id);

			myCursor.moveToNext();

		}
		myCursor.close();
	}
    public void onButtonClick(View view) {
    	
		switch (view.getId()) {
		
		case R.id.sf_antwort1:
			
			if(Antwort1.contains(AntwortMaster) && next<=ids.size()){
			next=next+1;
			Toast.makeText(this, "Richtig ", 30).show();
			ladeDaten();
			}else{
				Toast.makeText(this, "Falsch", 30).show();
		}
			if(next>ids.size()){
//				startActivity(new Intent(this, Main.class));
			}
			break;
		case R.id.sf_antwort2:
			if(Antwort2.contains(AntwortMaster) && next<=ids.size()){
			next=next+1;
			Toast.makeText(this, "Richtig", 30).show();
			ladeDaten();
		
			}else{
				Toast.makeText(this, "Falsch", 30).show();
			}
			if(next>ids.size()){
//				startActivity(new Intent(this, Main.class));
			}
			break;
		case R.id.sf_antwort3:
			if(Antwort3.contains(AntwortMaster)&& next<=ids.size()){
			next=next+1;
			Toast.makeText(this, "Richtig", 30).show();
			ladeDaten();
			
			}else{
				Toast.makeText(this, "Falsch", 30).show();
			if(next>ids.size()){
//				startActivity(new Intent(this, Main.class));
			}
		}
			break;
		case R.id.sf_antwort4:
			if(Antwort4.contains(AntwortMaster)&& next<=ids.size()){
			next=next+1;
			Toast.makeText(this, "Richtig", 30).show();
			ladeDaten();
			}else{
				Toast.makeText(this, "Falsch", 30).show();
			}
			if(next>ids.size()){
//				startActivity(new Intent(this, Main.class));
			}
			break;
		
		}

}
    private void fillDatabase(){
    	ContentValues werte = new ContentValues();
    	
    	Frage="Benita ist..";
    	Antwort1="ein Hund";
    	Antwort2="eine Katze";
    	Antwort3="eine Maus";
    	Antwort4= "ein Nilpferd";
    	AntwortMaster="ein Hund";
    	
    	werte.put("frage", Frage);
		werte.put("antwort1", Antwort1);
		werte.put("antwort2", Antwort2);
		werte.put("antwort3", Antwort3);
		werte.put("antwort4", Antwort4);
		werte.put("antwortmaster", AntwortMaster);
		
		mDatenbank.insert("quiz", null, werte);
		
		Frage="Dies ist ein Test..";
    	Antwort1="ein Hund";
    	Antwort2="eine Katze";
    	Antwort3="eine Maus";
    	Antwort4= "ein Nilpferd";
    	AntwortMaster="ein Hund";
    	
    	werte.put("frage", Frage);
		werte.put("antwort1", Antwort1);
		werte.put("antwort2", Antwort2);
		werte.put("antwort3", Antwort3);
		werte.put("antwort4", Antwort4);
		werte.put("antwortmaster", AntwortMaster);
		
		mDatenbank.insert("quiz", null, werte);
		
		Frage="Dies ist ein Test Zwei..";
    	Antwort1="ein Hund";
    	Antwort2="eine Katze";
    	Antwort3="eine Maus";
    	Antwort4= "ein Nilpferd";
    	AntwortMaster="ein Hund";
    	
    	werte.put("frage", Frage);
		werte.put("antwort1", Antwort1);
		werte.put("antwort2", Antwort2);
		werte.put("antwort3", Antwort3);
		werte.put("antwort4", Antwort4);
		werte.put("antwortmaster", AntwortMaster);
		
		mDatenbank.insert("quiz", null, werte);

		
		
    }
    
}
